﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
using QingZhouLinYe_Web.DbContext;

namespace QingZhouLinYe_Web
{
    public class BaseClass
    {
        public static string DbCoon= @"Data Source=9865mz1425az.vicp.fun,13755;Initial Catalog=QingZhouLinYe_test;Persist Security Info=True;User ID=sa;Password=Nap650;MultipleActiveResultSets=True;";
        /// <summary>
        /// 将不缩进的 JSON 字符串转成缩进形式
        /// </summary>
        /// <param name="str">Json字符串</param>
        /// <returns></returns>
        public static string JsonIndentation(string str)
        {
            //string str = JsonConvert.SerializeObject(entity);
            JsonSerializer serializer = new JsonSerializer();
            TextReader tr = new StringReader(str);
            JsonTextReader jtr = new JsonTextReader(tr);
            object obj = serializer.Deserialize(jtr);
            if (obj != null)
            {
                StringWriter textWriter = new StringWriter();
                JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
                {
                    Formatting = Formatting.Indented,
                    Indentation = 4,
                    IndentChar = ' '
                };
                serializer.Serialize(jsonWriter, obj);
                return textWriter.ToString();
            }
            else
            {
                return str;
            }
        }

        //获取经纬度
        public static Location returnLocation(string str)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return null;
            }

            var parts = str.Replace("POINT(", "").Replace("POINT (", "").Replace("(", "").Replace(")", "").Split(' ');
            if (parts.Length == 3 &&
                double.TryParse(parts[0], out var x) &&
                double.TryParse(parts[1], out var y) &&
                double.TryParse(parts[2], out var z))
            {
                var location = new Location(x, y, z);
                return location;
    }
            return null;
        }

        /// <summary>
        /// 产生随机小数
        /// </summary>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        public static double RandomDouble(Random ran,double minValue, double maxValue)
        {
            return ran.NextDouble() * (maxValue - minValue) + minValue;
        }

        /// <summary>
        /// 产生随机整数
        /// </summary>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        public static double RandomInt(Random ran,int minValue, int maxValue)
        {
            return ran.Next (minValue,maxValue+1);
        }

        

    }
}